{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "*  **深度学习**离不开数学分析（高等数学）、线性代数、概率论等知识，**更离不开以编程为核心的动手实践。**\n",
    "\n",
    "* 无论是在机器学习还是深度学习中，**Python** 已经成为主导性的编程语言。而且，现在许多主流的深度学习框架都提供Python接口，Python被用于数据预处理、定义网络模型、执行训练过程、数据可视化等\n",
    "\n",
    "* **熟悉 Python 的基础语法，并掌握 NumPy，Pandas 及其他基础工具模块的使用对深度学习实践是非常重要的！**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "**目录**\n",
    "\n",
    "*  Python数据结构\n",
    " \n",
    "*  Python面向对象\n",
    " \n",
    "*  Python JSON\n",
    " \n",
    "*  Python 异常处理\n",
    " \n",
    "*  常见Linux命令\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Python数据结构\n",
    "\n",
    "数字、字符串、列表、元祖、字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 数字\n",
    "\n",
    "Python Number 数据类型用于存储数值。\n",
    "\n",
    "Python Number 数据类型用于存储数值，包括整型、长整型、浮点型、复数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "**（1）Python math 模块**：Python 中数学运算常用的函数基本都在 math 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "4\n",
      "10.0\n",
      "3.0\n",
      "2.718281828459045\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "print(math.ceil(4.1))   #返回数字的上入整数\n",
    "\n",
    "print(math.floor(4.9))  #返回数字的下舍整数\n",
    "\n",
    "print(math.fabs(-10))   #返回数字的绝对值\n",
    "\n",
    "print(math.sqrt(9))     #返回数字的平方根\n",
    "\n",
    "print(math.exp(1))      #返回e的x次幂\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "**（2）Python随机数**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "首先import random，使用random()方法即可随机生成一个[0,1)范围内的实数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.09865340765469133\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "ran = random.random()\n",
    "print(ran)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "调用 random.random() 生成随机数时，每一次生成的数都是随机的。但是，当预先使用 random.seed(x) 设定好种子之后，其中的 x 可以是任意数字，此时使用 random() 生成的随机数将会是同一个。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------- 设置种子 seed -------\n",
      "Random number with seed 10 :  0.5714025946899135\n",
      "Random number with seed 10 :  0.5714025946899135\n"
     ]
    }
   ],
   "source": [
    "print (\"------- 设置种子 seed -------\")\n",
    "random.seed(10)\n",
    "print (\"Random number with seed 10 : \", random.random())\n",
    "\n",
    "# 生成同一个随机数\n",
    "random.seed(10)\n",
    "print (\"Random number with seed 10 : \", random.random())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "randint()生成一个随机整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "\n",
    "ran = random.randint(1,20)\n",
    "print(ran)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "字符串连接：+"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World \n"
     ]
    }
   ],
   "source": [
    "a = \"Hello \"\n",
    "b = \"World \"\n",
    "print(a + b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "重复输出字符串：*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Hello Hello \n"
     ]
    }
   ],
   "source": [
    "print(a * 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "通过索引获取字符串中字符[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n"
     ]
    }
   ],
   "source": [
    "print(a[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "字符串截取[:]  牢记：左开右闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ell\n"
     ]
    }
   ],
   "source": [
    "print(a[1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "判断字符串中是否包含给定的字符: in, not in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print('e' in a)\n",
    "print('e' not in a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "join():以字符作为分隔符，将字符串中所有的元素合并为一个新的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H-e-l-l-o\n"
     ]
    }
   ],
   "source": [
    "new_str = '-'.join('Hello')\n",
    "print(new_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "字符串单引号、双引号、三引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n",
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "print('Hello World!')\n",
    "print(\"Hello World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "转义字符 \\"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The \t is a tab\n",
      "I'm going to the movies\n"
     ]
    }
   ],
   "source": [
    "print(\"The \\t is a tab\")\n",
    "print('I\\'m going to the movies')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "三引号让程序员从引号和特殊字符串的泥潭里面解脱出来，自始至终保持一小块字符串的格式是所谓的WYSIWYG（**所见即所得**）格式的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm going to the movies\n",
      "\n",
      "<HTML><HEAD><TITLE>\n",
      "Friends CGI Demo</TITLE></HEAD>\n",
      "<BODY><H3>ERROR</H3>\n",
      "<B>%s</B><P>\n",
      "<FORM><INPUT TYPE=button VALUE=Back\n",
      "ONCLICK=\"window.history.back()\"></FORM>\n",
      "</BODY></HTML>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print('''I'm going to the movies''')\n",
    "\n",
    "html = '''\n",
    "<HTML><HEAD><TITLE>\n",
    "Friends CGI Demo</TITLE></HEAD>\n",
    "<BODY><H3>ERROR</H3>\n",
    "<B>%s</B><P>\n",
    "<FORM><INPUT TYPE=button VALUE=Back\n",
    "ONCLICK=\"window.history.back()\"></FORM>\n",
    "</BODY></HTML>\n",
    "'''\n",
    "print(html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 列表\n",
    "\n",
    "作用：类似其他语言中的数组\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "声明一个列表，并通过下标或索引获取元素\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/41840090b1c14c4daa704f9565fd5ba663d08a7d383444fa98c5c3d31f7e4c1d)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jack\n",
      "tom\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#声明一个列表\n",
    "names = ['jack','tom','tonney','superman','jay']\n",
    "\n",
    "#通过下标或索引获取元素\n",
    "print(names[0])\n",
    "print(names[1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jay\n",
      "jay\n"
     ]
    }
   ],
   "source": [
    "#获取最后一个元素\n",
    "print(names[-1])\n",
    "print(names[len(names)-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jack\n"
     ]
    }
   ],
   "source": [
    "#获取第一个元素\n",
    "print(names[-5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jack\n",
      "tom\n",
      "tonney\n",
      "superman\n",
      "jay\n"
     ]
    }
   ],
   "source": [
    "#遍历列表，获取元素\n",
    "for name in names:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "有超人\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#查询names里面有没有superman\n",
    "for name in names:\n",
    "    if name == 'superman':\n",
    "        print('有超人')\n",
    "        break\n",
    "else:\n",
    "    print('有超人')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "有超人\n"
     ]
    }
   ],
   "source": [
    "#更简单的方法,来查询names里有没有superman\n",
    "if 'superman' in names:\n",
    "    print('有超人')\n",
    "else:\n",
    "    print('有超人')\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "列表元素添加\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['杨超越']\n"
     ]
    }
   ],
   "source": [
    "#声明一个空列表\n",
    "girls = []\n",
    "\n",
    "#append(),末尾追加\n",
    "girls.append('杨超越')\n",
    "print(girls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['杨超越', '刘雯', '奚梦瑶']\n"
     ]
    }
   ],
   "source": [
    "#extend(),一次添加多个。把一个列表添加到另一个列表 ，列表合并。\n",
    "models = ['刘雯','奚梦瑶']\n",
    "girls.extend(models)\n",
    "#girls = girls + models\n",
    "print(girls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['杨超越', '虞书欣', '刘雯', '奚梦瑶']\n"
     ]
    }
   ],
   "source": [
    "#insert():指定位置添加\n",
    "girls.insert(1,'虞书欣')\n",
    "print(girls)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "列表元素修改,通过下标找到元素，然后用=赋值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'pear', '香蕉', 'pineapple', '草莓']\n",
      "['apple', 'pear', '香蕉', 'pineapple', 'strawberry']\n"
     ]
    }
   ],
   "source": [
    "fruits = ['apple','pear','香蕉','pineapple','草莓']\n",
    "print(fruits)\n",
    "\n",
    "fruits[-1] = 'strawberry'\n",
    "print(fruits)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'pear', '香蕉', 'pineapple', 'strawberry']\n",
      "['apple', 'pear', 'banana', 'pineapple', 'strawberry']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "'''\n",
    "将fruits列表中的‘香蕉’替换为‘banana’\n",
    "'''\n",
    "\n",
    "for fruit in fruits:\n",
    "    if '香蕉' in fruit:\n",
    "        fruit = 'banana'\n",
    "print(fruits)\n",
    "\n",
    "for i in range(len(fruits)):\n",
    "    if '香蕉' in fruits[i]:\n",
    "        fruits[i] = 'banana'\n",
    "        break\n",
    "print(fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "列表元素删除\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cat', 'pen', 'pencil', 'ruler']\n"
     ]
    }
   ],
   "source": [
    "words = ['cat','hello','pen','pencil','ruler']\n",
    "del words[1]\n",
    "print(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'pen', 'pencil', 'ruler']\n"
     ]
    }
   ],
   "source": [
    "words = ['cat','hello','pen','pencil','ruler']\n",
    "words.remove('cat')\n",
    "print(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cat', 'pen', 'pencil', 'ruler']\n"
     ]
    }
   ],
   "source": [
    "words = ['cat','hello','pen','pencil','ruler']\n",
    "words.pop(1)\n",
    "print(words)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "列表切片\n",
    "\n",
    "* 在Python中处理列表的部分元素，称之为切片。\n",
    "\n",
    "* 创建切片，可指定要使用的第一个元素和最后一个元素的索引。注意：左开右闭\n",
    "\n",
    "* 将截取的结果再次存放在一个列表中，所以还是返回列表\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/3e1286d72f144f378afd29d8fc720e1430919c5b0c0d413b9ca1539f3ae087a0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['tiger', 'snake', 'mouse']\n",
      "['bird']\n",
      "['snake', 'mouse']\n",
      "['dog', 'snake']\n",
      "['cat', 'tiger', 'mouse']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "animals = ['cat','dog','tiger','snake','mouse','bird']\n",
    "\n",
    "print(animals[2:5])\n",
    "\n",
    "print(animals[-1:])\n",
    "\n",
    "print(animals[-3:-1])\n",
    "\n",
    "print(animals[-5:-1:2])\n",
    "\n",
    "print(animals[::2])\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "列表排序\n",
    "\n",
    "* 随机生成10个不同的整数，并进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[16, 19, 1, 7, 15, 9, 6, 2, 17]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "生成10个不同的随机整数，并存至列表中\n",
    "'''\n",
    "import  random\n",
    "\n",
    "random_list = []\n",
    "for i in range(10):\n",
    "    ran = random.randint(1,20)\n",
    "    if ran not in  random_list:\n",
    "        random_list.append(ran)\n",
    "print(random_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "上述代码存在什么问题吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[16, 11, 3, 8, 12, 2, 14, 5, 20, 13]\n"
     ]
    }
   ],
   "source": [
    "import  random\n",
    "\n",
    "random_list = []\n",
    "i = 0\n",
    "while i < 10:\n",
    "    ran = random.randint(1,20)\n",
    "    if ran not in  random_list:\n",
    "        random_list.append(ran)\n",
    "        i+=1\n",
    "print(random_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 8, 11, 12, 13, 14, 16, 20]\n",
      "[20, 16, 14, 13, 12, 11, 8, 5, 3, 2]\n"
     ]
    }
   ],
   "source": [
    "#默认升序\n",
    "new_list = sorted(random_list)\n",
    "print(new_list)\n",
    "\n",
    "#降序\n",
    "new_list = sorted(random_list,reverse =True)\n",
    "print(new_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 元组\n",
    "与列表类似，元祖中的内容不可修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "tuple1 = ()\n",
    "print(type(tuple1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "tuple2 = ('hello')\n",
    "print(type(tuple2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "注意：元组中只有一个元素时，需要在后面加逗号！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "\n",
    "tuple3 = ('hello',)\n",
    "print(type(tuple3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "元组不能修改，所以不存在往元组里加入元素。\n",
    "\n",
    "那作为容器的元组，如何存放元素？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[14, 10, 9, 15, 6, 10, 12, 5, 15, 8]\n",
      "(14, 10, 9, 15, 6, 10, 12, 5, 15, 8)\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "random_list = []\n",
    "for i in range(10):\n",
    "    ran = random.randint(1,20)\n",
    "    random_list.append(ran)\n",
    "print(random_list)\n",
    "\n",
    "random_tuple = tuple(random_list)\n",
    "print(random_tuple)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<br/>\n",
    "\n",
    "元组访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(14, 10, 9, 15, 6, 10, 12, 5, 15, 8)\n",
      "14\n",
      "8\n",
      "(10, 9, 15, 6, 10, 12)\n",
      "(8, 15, 5, 12, 10, 6, 15, 9, 10, 14)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(random_tuple)\n",
    "print(random_tuple[0])\n",
    "print(random_tuple[-1])\n",
    "print(random_tuple[1:-3])\n",
    "print(random_tuple[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "元组的修改："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "t1 = (1,2,3)+(4,5)\n",
    "print(t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 1, 2)\n"
     ]
    }
   ],
   "source": [
    "t2 = (1,2) * 2\n",
    "print(t2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "元组的一些函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "5\n",
      "104\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "print(max(random_tuple))\n",
    "print(min(random_tuple))\n",
    "print(sum(random_tuple))\n",
    "print(len(random_tuple))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "#统计元组中4的个数\n",
    "print(random_tuple.count(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "tuple.index(x): x not in tuple",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-41-82b4b5455c6b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#元组中4所对应的下标，如果不存在，则会报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrandom_tuple\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: tuple.index(x): x not in tuple"
     ]
    }
   ],
   "source": [
    "#元组中4所对应的下标，如果不存在，则会报错\n",
    "print(random_tuple.index(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "#判断元组中是否存在1这个元素\n",
    "print(4 in random_tuple)\n",
    "\n",
    "#返回元组中4所对应的下标,不会报错\n",
    "if(4 in random_tuple):\n",
    "    print(random_tuple.index(4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "元组的拆包与装包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "#定义一个元组\n",
    "t3 = (1,2,3)\n",
    "\n",
    "#将元组赋值给变量a,b,c\n",
    "a,b,c = t3\n",
    "\n",
    "#打印a,b,c\n",
    "print(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 [3, 4, 5]\n",
      "[3, 4, 5]\n",
      "3 4 5\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#当元组中元素个数与变量个数不一致时\n",
    "\n",
    "#定义一个元组，包含5个元素\n",
    "t4 = (1,2,3,4,5)\n",
    "\n",
    "#将t4[0],t4[1]分别赋值给a,b;其余的元素装包后赋值给c\n",
    "a,b,*c = t4\n",
    "\n",
    "print(a,b,c)\n",
    "print(c)\n",
    "print(*c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "杨超越\n"
     ]
    }
   ],
   "source": [
    "#定义一个空字典\n",
    "\n",
    "dict1 = {}\n",
    "\n",
    "dict2 = {'name':'杨超越','weight':45,'age':25}\n",
    "print(dict2['name'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '杨超越', 'weight': 45}\n"
     ]
    }
   ],
   "source": [
    "#list可以转成字典，但前提是列表中元素都要成对出现\n",
    "dict3 = dict([('name','杨超越'),('weight',45)])\n",
    "print(dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '虞书欣', 'weight': 43}\n"
     ]
    }
   ],
   "source": [
    "dict4 = {}\n",
    "dict4['name'] = '虞书欣'\n",
    "dict4['weight'] = 43\n",
    "print(dict4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '虞书欣', 'weight': 44}\n"
     ]
    }
   ],
   "source": [
    "dict4['weight'] = 44\n",
    "print(dict4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('杨超越', 165), ('虞书欣', 166), ('上官喜爱', 164)])\n",
      "虞书欣\n"
     ]
    }
   ],
   "source": [
    "#字典里的函数 items()  keys() values()\n",
    "\n",
    "dict5 = {'杨超越':165,'虞书欣':166,'上官喜爱':164}\n",
    "print(dict5.items())\n",
    "for key,value in dict5.items():\n",
    "    if value > 165:\n",
    "        print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([165, 166, 164])\n"
     ]
    }
   ],
   "source": [
    "#values() 取出字典中所有的值,保存到列表中\n",
    "\n",
    "results = dict5.values()\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values([165, 166, 164])\n",
      "165.0\n"
     ]
    }
   ],
   "source": [
    "#求小姐姐的平均身高\n",
    "heights = dict5.values()\n",
    "print(heights)\n",
    "total = sum(heights)\n",
    "avg = total/len(heights)\n",
    "print(avg)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['杨超越', '虞书欣', '上官喜爱'])\n"
     ]
    }
   ],
   "source": [
    "names = dict5.keys()\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "170\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#print(dict5['赵小棠'])       \n",
    "\n",
    "print(dict5.get('赵小棠'))\n",
    "\n",
    "print(dict5.get('赵小棠',170)) #如果能够取到值，则返回字典中的值，否则返回默认值170\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'虞书欣': 166, '上官喜爱': 164}\n"
     ]
    }
   ],
   "source": [
    "dict6 = {'杨超越':165,'虞书欣':166,'上官喜爱':164}\n",
    "del dict6['杨超越']\n",
    "print(dict6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "166\n",
      "{'上官喜爱': 164}\n"
     ]
    }
   ],
   "source": [
    "result = dict6.pop('虞书欣')\n",
    "print(result)\n",
    "print(dict6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Python面向对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "定义一个类Animals:\n",
    "\n",
    "(1)__init__()定义构造函数，与其他面向对象语言不同的是，Python语言中，会明确地把代表自身实例的self作为第一个参数传入\n",
    "\n",
    "(2)创建一个实例化对象 cat，__init__()方法接收参数\n",
    "\n",
    "(3)使用点号 . 来访问对象的属性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "动物名称实例化\n",
      "miaomiao\n",
      "miaomiao要吃东西啦！\n",
      "miaomiao要喝水啦！\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "\n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "        print('动物名称实例化')\n",
    "    def eat(self):\n",
    "        print(self.name +'要吃东西啦！')\n",
    "    def drink(self):\n",
    "        print(self.name +'要喝水啦！')\n",
    "\n",
    "cat =  Animal('miaomiao')\n",
    "print(cat.name)\n",
    "cat.eat()\n",
    "cat.drink()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用子类构造方法\n",
      "调用子类方法\n",
      "调用父类方法\n"
     ]
    }
   ],
   "source": [
    "class Person:        \n",
    "    def __init__(self,name):\n",
    "        self.name = name\n",
    "        print ('调用父类构造函数')\n",
    "\n",
    "    def eat(self):\n",
    "        print('调用父类方法')\n",
    " \n",
    "class Student(Person):  # 定义子类\n",
    "   def __init__(self):\n",
    "      print ('调用子类构造方法')\n",
    " \n",
    "   def study(self):\n",
    "      print('调用子类方法')\n",
    "\n",
    "s = Student()          # 实例化子类\n",
    "s.study()              # 调用子类的方法\n",
    "s.eat()                # 调用父类方法\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Python JSON"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式，易于人阅读和编写。\n",
    "\n",
    "json.dumps 用于将 Python 对象编码成 JSON 字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"b\": 2, \"d\": 4, \"a\": 1, \"c\": 3, \"e\": 5}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "data = [ { 'b' : 2, 'd' : 4, 'a' : 1, 'c' : 3, 'e' : 5 } ]\n",
    "json = json.dumps(data)\n",
    "print(json)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "为了提高可读性，dumps方法提供了一些可选的参数。\n",
    "\n",
    "sort_keys=True表示按照字典排序(a到z)输出。\n",
    "\n",
    "indent参数，代表缩进的位数\n",
    "\n",
    "separators参数的作用是去掉,和:后面的空格，传输过程中数据越精简越好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    {\n",
      "        \"a\":1,\n",
      "        \"b\":2,\n",
      "        \"c\":3,\n",
      "        \"d\":4,\n",
      "        \"e\":5\n",
      "    }\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "data = [ { 'b' : 2, 'd' : 4, 'a' : 1, 'c' : 3, 'e' : 5 } ]\n",
    "json = json.dumps(data, sort_keys=True, indent=4,separators=(',', ':'))\n",
    "print(json)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "jsonData = '{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}'\n",
    "text = json.loads(jsonData)  #将string转换为dict\n",
    "print(text)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Python异常处理\n",
    "\n",
    "当Python脚本发生异常时我们需要捕获处理它，否则程序会终止执行。\n",
    "\n",
    "捕捉异常可以使用try/except语句。\n",
    "\n",
    "try/except语句用来检测try语句块中的错误，从而让except语句捕获异常信息并处理。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内容写入文件成功\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    fh = open(\"/home/aistudio/data/testfile01.txt\", \"w\")\n",
    "    fh.write(\"这是一个测试文件，用于测试异常!!\")\n",
    "except IOError:\n",
    "    print('Error: 没有找到文件或读取文件失败')\n",
    "else:\n",
    "    print ('内容写入文件成功')\n",
    "    fh.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "finally中的内容，退出try时总会执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "关闭文件\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    f = open(\"/home/aistudio/data/testfile02.txt\", \"w\")\n",
    "    f.write(\"这是一个测试文件，用于测试异常!!\")\n",
    "finally:\n",
    "    print('关闭文件')\n",
    "    f.close()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 常见Linux命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aistudio\r\n"
     ]
    }
   ],
   "source": [
    "!ls /home"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1627136.ipynb  data  work\r\n"
     ]
    }
   ],
   "source": [
    "!ls ./"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 52\r\n",
      "-rw-r--r-- 1 aistudio users 41755 Mar  8 15:47 1627136.ipynb\r\n",
      "drwxr-xr-x 2 aistudio users  4096 Mar  8 15:57 \u001b[0m\u001b[01;34mdata\u001b[0m/\r\n",
      "drwxr-xr-x 2 aistudio users  4096 Mar  8 15:46 \u001b[01;34mwork\u001b[0m/\r\n"
     ]
    }
   ],
   "source": [
    "ls  -l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/aistudio\r\n"
     ]
    }
   ],
   "source": [
    "!pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "cp ：复制文件或目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cp: cannot stat 'test.txt': No such file or directory\r\n"
     ]
    }
   ],
   "source": [
    "!cp test.txt ./test_copy.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "mv:移动文件与目录，或修改文件与目录的名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mv: cannot stat '/home/aistudio/work/test_copy.txt': No such file or directory\r\n"
     ]
    }
   ],
   "source": [
    "!mv /home/aistudio/work/test_copy.txt /home/aistudio/data/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "rm :移除文件或目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rm: cannot remove '/home/aistudio/data/test_copy.txt': No such file or directory\r\n"
     ]
    }
   ],
   "source": [
    "!rm /home/aistudio/data/test_copy.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "很多大型文件或者数据从服务器上传或者下载的时候都需要打包和压缩解压，这时候知道压缩和解压的各种命令是很有必要的。\n",
    "\n",
    "常见的压缩文件后缀名有.tar.gz，.gz，和.zip，下面来看看在Linux上它们分别的解压和压缩命令。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## gzip:\n",
    "\n",
    "linux压缩文件中最常见的后缀名即为.gz，gzip是用来压缩和解压.gz文件的命令。\n",
    "\n",
    "常用参数:\n",
    "\n",
    "    -d或--decompress或--uncompress：解压文件；\n",
    "    -r或--recursive：递归压缩指定文件夹下的文件（该文件夹下的所有文件被压缩成单独的.gz文件）；\n",
    "    -v或--verbose：显示指令执行过程。\n",
    "    注：gzip命令只能压缩单个文件，而不能把一个文件夹压缩成一个文件（与打包命令的区别）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gzip: /home/aistudio/work/test.txt: No such file or directory\r\n"
     ]
    }
   ],
   "source": [
    "#会将文件压缩为文件 test.txt.gz，原来的文件则没有了，解压缩也一样\n",
    "!gzip /home/aistudio/work/test.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gzip: /home/aistudio/test.gz: No such file or directory\r\n"
     ]
    }
   ],
   "source": [
    "!gzip -d /home/aistudio/test.gz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## tar:\n",
    "\n",
    "tar本身是一个打包命令，用来打包或者解包后缀名为.tar。配合参数可同时实现打包和压缩。\n",
    "\n",
    "常用参数:\n",
    "\n",
    "    -c或--create：建立新的备份文件；\n",
    "    -x或--extract或--get：从备份文件中还原文件；\n",
    "    -v：显示指令执行过程；\n",
    "    -f或--file：指定备份文件；\n",
    "    -C：指定目的目录；\n",
    "    -z：通过gzip指令处理备份文件；\n",
    "    -j：通过bzip2指令处理备份文件。\n",
    "\n",
    "最常用的是将tar命令与gzip命令组合起来，直接对文件夹先打包后压缩："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tar: Removing leading `/' from member names\r\n",
      "tar: /home/aistudio/work/test.txt: Cannot stat: No such file or directory\r\n",
      "tar: Exiting with failure status due to previous errors\r\n"
     ]
    }
   ],
   "source": [
    "!tar -zcvf /home/aistudio/work/test.tar.gz /home/aistudio/work/test.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "!tar -zxvf /home/aistudio/work/test.tar.gz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## zip和unzip\n",
    "\n",
    "zip命令和unzip命令用在在Linux上处理.zip的压缩文件。\n",
    "\n",
    "常用参数\n",
    "    \n",
    "zip:\n",
    "    \n",
    "    -v：显示指令执行过程；\n",
    "    -m：不保留原文件；\n",
    "    -r：递归处理。\n",
    "    \n",
    "unzip:\n",
    "    \n",
    "    -v：显示指令执行过程；\n",
    "    -d：解压到指定目录。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tzip warning: name not matched: /home/aistudio/work/test.txt\r\n",
      "\r\n",
      "zip error: Nothing to do! (try: zip -r /home/aistudio/work/test.zip . -i /home/aistudio/work/test.txt)\r\n"
     ]
    }
   ],
   "source": [
    "!zip -r /home/aistudio/work/test.zip /home/aistudio/work/test.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unzip:  cannot find or open /home/aistudio/work/test.zip, /home/aistudio/work/test.zip.zip or /home/aistudio/work/test.zip.ZIP.\r\n"
     ]
    }
   ],
   "source": [
    "!unzip  /home/aistudio/work/test.zip "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 1.7.2 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
